//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "MakeSwaption.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Indexes/SwapIndex.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Instruments/Swaption.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CMakeSwaption::CMakeSwaption (Cephei::QL::Indexes::ISwapIndex^ swapIndex, Cephei::QL::Times::IPeriod^ optionTenor, Microsoft::FSharp::Core::FSharpOption<Double>^ strike) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CSwapIndex^ _CswapIndex;
    CPeriod^ _CoptionTenor;
    try
    {
#ifdef HANDLE
        _phMakeSwaption = NULL;
#endif
        _CswapIndex = safe_cast<CSwapIndex^> (swapIndex);
        _CswapIndex->Lock();
        boost::shared_ptr<QuantLib::SwapIndex>& _swapIndex = static_cast<boost::shared_ptr<QuantLib::SwapIndex>&> (_CswapIndex->GetShared ()); 
        _CoptionTenor = safe_cast<CPeriod^> (optionTenor);
        _CoptionTenor->Lock();
        QuantLib::Period& _optionTenor = static_cast<QuantLib::Period&> (_CoptionTenor->GetReference ()); 
        QuantLib::Rate _strike = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (strike) ? (QuantLib::Rate)ValueHelper::Convert (strike->Value) : Null<QuantLib::Rate>()); //4
        _ppMakeSwaption = new boost::shared_ptr<QuantLib::MakeSwaption> (new QuantLib::MakeSwaption ( _swapIndex,  _optionTenor,  _strike ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CswapIndex != nullptr) _CswapIndex->Unlock();
        if (_CoptionTenor != nullptr) _CoptionTenor->Unlock();
    }
}
Cephei::QL::Instruments::CMakeSwaption::CMakeSwaption (boost::shared_ptr<QuantLib::MakeSwaption>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMakeSwaption = NULL;
#endif
	_ppMakeSwaption = &childNative;
    
}
Cephei::QL::Instruments::CMakeSwaption::CMakeSwaption (QuantLib::MakeSwaption& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phMakeSwaption = NULL;
#endif
	_ppMakeSwaption = new boost::shared_ptr<QuantLib::MakeSwaption> (&childNative);
    
    _MakeSwaptionOwner = owner;
    
}

Cephei::QL::Instruments::CMakeSwaption::CMakeSwaption (CMakeSwaption^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMakeSwaption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMakeSwaption = new boost::shared_ptr<QuantLib::MakeSwaption> (copy->GetShared());
        
    }
}
Cephei::QL::Instruments::CMakeSwaption::CMakeSwaption (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMakeSwaption = NULL;
#endif
	if (!t->IsSubclassOf(CMakeSwaption::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CMakeSwaption::CMakeSwaption (QuantLib::Handle<QuantLib::MakeSwaption>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMakeSwaption = &childNative;
	_ppMakeSwaption = &static_cast<boost::shared_ptr<QuantLib::MakeSwaption>>(childNative.currentLink());
    
    _MakeSwaptionOwner = owner;
}
Cephei::QL::Instruments::CMakeSwaption::CMakeSwaption (QuantLib::Handle<QuantLib::MakeSwaption> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMakeSwaption = &childNative;
	_ppMakeSwaption = &static_cast<boost::shared_ptr<QuantLib::MakeSwaption>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CMakeSwaption::CMakeSwaption (QuantLib::MakeSwaption childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMakeSwaption = NULL;
#endif
	_ppMakeSwaption = new boost::shared_ptr<QuantLib::MakeSwaption> (new QuantLib::MakeSwaption (childNative));
    
}
#endif

Cephei::QL::Instruments::CMakeSwaption::~CMakeSwaption ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppMakeSwaption != NULL)
    {
	    delete _ppMakeSwaption;
        _ppMakeSwaption = NULL;
    }
}
Cephei::QL::Instruments::CMakeSwaption::!CMakeSwaption ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppMakeSwaption != NULL)
    {
	    delete _ppMakeSwaption;
    }
}
QuantLib::MakeSwaption& Cephei::QL::Instruments::CMakeSwaption::GetReference ()
{
    if (_ppMakeSwaption == NULL) throw gcnew NativeNullException ();
	return **_ppMakeSwaption;
}
boost::shared_ptr<QuantLib::MakeSwaption>& Cephei::QL::Instruments::CMakeSwaption::GetShared ()
{
    if (_ppMakeSwaption == NULL) throw gcnew NativeNullException ();
	return *_ppMakeSwaption;
}
QuantLib::MakeSwaption* Cephei::QL::Instruments::CMakeSwaption::GetPointer ()
{
    if (_ppMakeSwaption == NULL) throw gcnew NativeNullException ();
	return &**_ppMakeSwaption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MakeSwaption>& Cephei::QL::Instruments::CMakeSwaption::GetHandle ()
{
	if (_phMakeSwaption == NULL)
	{
		_phMakeSwaption = new Handle<QuantLib::MakeSwaption> (*_ppMakeSwaption);
	}
	return *_phMakeSwaption;
}
#endif
bool Cephei::QL::Instruments::CMakeSwaption::HasNative () 
{
	return (_ppMakeSwaption != NULL);
}

Cephei::QL::Instruments::IMakeSwaption^ Cephei::QL::Instruments::CMakeSwaption::WithOptionConvention (QL::Times::BusinessDayConventionEnum bdc)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::BusinessDayConvention _bdc = (QuantLib::BusinessDayConvention)bdc ;
    	QuantLib::MakeSwaption& _rv = (QuantLib::MakeSwaption&)(*_ppMakeSwaption)->withOptionConvention ( _bdc );   
        Cephei::QL::Instruments::CMakeSwaption^ _nrv = gcnew Cephei::QL::Instruments::CMakeSwaption (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::IMakeSwaption^ Cephei::QL::Instruments::CMakeSwaption::WithPricingEngine (Cephei::QL::IPricingEngine^ engine)
{
    CPricingEngine^ _Cengine;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cengine = safe_cast<CPricingEngine^> (engine);
        _Cengine->Lock();
        boost::shared_ptr<QuantLib::PricingEngine>& _engine = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_Cengine->GetShared ()); 
    	QuantLib::MakeSwaption& _rv = (QuantLib::MakeSwaption&)(*_ppMakeSwaption)->withPricingEngine ( _engine );   
        Cephei::QL::Instruments::CMakeSwaption^ _nrv = gcnew Cephei::QL::Instruments::CMakeSwaption (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cengine != nullptr) _Cengine->Unlock();
    }
}
Cephei::QL::Instruments::IMakeSwaption^ Cephei::QL::Instruments::CMakeSwaption::WithSettlementType (QL::Instruments::Settlement::TypeEnum delivery)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Settlement::Type _delivery = (QuantLib::Settlement::Type)delivery ;
    	QuantLib::MakeSwaption& _rv = (QuantLib::MakeSwaption&)(*_ppMakeSwaption)->withSettlementType ( _delivery );   
        Cephei::QL::Instruments::CMakeSwaption^ _nrv = gcnew Cephei::QL::Instruments::CMakeSwaption (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IMakeSwaption^ Cephei::QL::Instruments::CMakeSwaption_Factory::Create (Cephei::QL::Indexes::ISwapIndex^ swapIndex, Cephei::QL::Times::IPeriod^ optionTenor, Microsoft::FSharp::Core::FSharpOption<Double>^ strike)
{
    return gcnew CMakeSwaption ( swapIndex,  optionTenor,  strike);
}
